package com.luych.toolbox.automation.service.controller;

import com.luych.toolbox.automation.service.entity.AutoTaskTemplate;
import com.luych.toolbox.automation.service.service.AutoTaskTemplateService;
import com.luych.toolbox.common.constant.PageConstant;
import com.luych.toolbox.common.enums.AutoTaskTemplateArgType;
import com.luych.toolbox.common.enums.AutoTaskTemplateNodeStdinType;
import com.luych.toolbox.common.enums.AutoTaskTemplateNodeStdoutType;
import com.luych.toolbox.common.enums.AutoTaskTemplateNodeType;
import com.luych.toolbox.common.feign.intf.automation.service.AutoTaskTemplateFeign;
import com.luych.toolbox.common.feign.param.automation.service.AutoTaskTemplateParam;
import com.luych.toolbox.common.feign.view.automation.service.*;
import com.luych.toolbox.common.page.CommonPageParam;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.common.page.CommonSort;
import com.luych.toolbox.common.page.PageResult;
import com.luych.toolbox.common.service.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "任务模板")
@RestController
@RequestMapping("/template")
public class AutoTaskTemplateController
        extends BaseController<AutoTaskTemplateView, AutoTaskTemplateParam, AutoTaskTemplate>
        implements AutoTaskTemplateFeign {

    private final AutoTaskTemplateService taskTemplateService;

    @Autowired
    public AutoTaskTemplateController(AutoTaskTemplateService taskTemplateService) {
        super(taskTemplateService);
        this.taskTemplateService = taskTemplateService;
    }

    @ApiOperation("查询参数类型")
    @GetMapping("/type/arg")
    public CommonPageResult<AutoTaskTemplateArgTypeView> listArgType() {
        List<AutoTaskTemplateArgTypeView> views = new ArrayList<>();
        for (AutoTaskTemplateArgType type : AutoTaskTemplateArgType.values()) {
            AutoTaskTemplateArgTypeView view = new AutoTaskTemplateArgTypeView();
            view.setValue(type.getValue());
            view.setName(type.name());
            views.add(view);
        }
        CommonPageResult<AutoTaskTemplateArgTypeView> commonPageResult = new CommonPageResult<>();
        commonPageResult.setTotal(views.size());
        commonPageResult.setRecords(views);
        return commonPageResult;
    }

    @ApiOperation("查询节点类型")
    @GetMapping("/type/node")
    public CommonPageResult<AutoTaskTemplateNodeTypeView> listNodeType() {
        List<AutoTaskTemplateNodeTypeView> views = new ArrayList<>();
        for (AutoTaskTemplateNodeType type : AutoTaskTemplateNodeType.values()) {
            AutoTaskTemplateNodeTypeView view = new AutoTaskTemplateNodeTypeView();
            view.setValue(type.getValue());
            view.setName(type.name());
            views.add(view);
        }
        CommonPageResult<AutoTaskTemplateNodeTypeView> commonPageResult = new CommonPageResult<>();
        commonPageResult.setTotal(views.size());
        commonPageResult.setRecords(views);
        return commonPageResult;
    }

    @ApiOperation("查询节点输入类型")
    @GetMapping("/type/stdin")
    public CommonPageResult<AutoTaskTemplateNodeStdinTypeView> listNodeStdinType() {
        List<AutoTaskTemplateNodeStdinTypeView> views = new ArrayList<>();
        for (AutoTaskTemplateNodeStdinType type : AutoTaskTemplateNodeStdinType.values()) {
            AutoTaskTemplateNodeStdinTypeView view = new AutoTaskTemplateNodeStdinTypeView();
            view.setValue(type.getValue());
            view.setName(type.name());
            views.add(view);
        }
        CommonPageResult<AutoTaskTemplateNodeStdinTypeView> commonPageResult = new CommonPageResult<>();
        commonPageResult.setTotal(views.size());
        commonPageResult.setRecords(views);
        return commonPageResult;
    }

    @ApiOperation("查询节点输出类型")
    @GetMapping("/type/stdout")
    public CommonPageResult<AutoTaskTemplateNodeStdoutTypeView> listNodeStdoutType() {
        List<AutoTaskTemplateNodeStdoutTypeView> views = new ArrayList<>();
        for (AutoTaskTemplateNodeStdoutType type : AutoTaskTemplateNodeStdoutType.values()) {
            AutoTaskTemplateNodeStdoutTypeView view = new AutoTaskTemplateNodeStdoutTypeView();
            view.setValue(type.getValue());
            view.setName(type.name());
            views.add(view);
        }
        CommonPageResult<AutoTaskTemplateNodeStdoutTypeView> commonPageResult = new CommonPageResult<>();
        commonPageResult.setTotal(views.size());
        commonPageResult.setRecords(views);
        return commonPageResult;
    }

    @ApiOperation("按关键字查询")
    @GetMapping("/search/keyword")
    public CommonPageResult<AutoTaskTemplateView> pageByKeyword(
            @ApiParam(value = "每页容量") @RequestHeader(name = "pageSize", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE_STR) long pageSize,
            @ApiParam(value = "查询页码") @RequestHeader(name = "pageNumber", required = false, defaultValue = PageConstant.DEFAULT_PAGE_NUMBER_STR) long pageNumber,
            @ApiParam(value = "排序字段") @RequestHeader(name = "sort", required = false, defaultValue = PageConstant.DEFAULT_PAGE_SORT) String sort,
            @ApiParam(value = "正序/倒序，取值ASC/DESC") @RequestHeader(name = "order", required = false, defaultValue = PageConstant.DEFAULT_PAGE_ORDER) String order,
            @ApiParam(value = "查询关键字，支持按名称和IP地址模糊查询") @RequestParam(name = "keyword", required = false) String keyword
    ) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<AutoTaskTemplateView> pageResult = taskTemplateService.pageByKeyword(commonPageParam, keyword);
        return CommonPageResult.getInstance(pageResult);
    }
}
